package ruifeng.zcj.communcationring.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.widget.ImageView;

import com.networkbench.agent.impl.NBSAppAgent;
import com.rey.material.widget.SnackBar;

import butterknife.ButterKnife;
import cn.pedant.SweetAlert.SweetAlertDialog;
import ruifeng.zcj.communcationring.R;
import ruifeng.zcj.communcationring.base.utils.TUtil;
import com.networkbench.agent.impl.NBSAppAgent;


/**
 * Created by Administrator on 2016/8/15 0015.
 */
public abstract class BaseActivity<T extends BasePresenter,E extends BaseModel> extends MySwipeBackActivity {
    public T mPresenter;
    public E mModel;
    public Context mContext;
//    public boolean isNight;
    private ImageView ivShadow;

    private SweetAlertDialog mLoadingDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(this.getLayoutId());
        ButterKnife.bind(this);

        mContext = this;
        mPresenter = TUtil.getT(this, 0);
        mModel = TUtil.getT(this, 1);
        this.initView();
        if (this instanceof BaseView) mPresenter.setVM(this, mModel);

        // 注册听云的检测分析
        NBSAppAgent.setLicenseKey("2affe551b58940748f81dfc6fa57f2b2")
                .withCrashReportEnabled(true).withLocationServiceEnabled(true)
                .start(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.onDestroy();
        ButterKnife.unbind(this);

    }
    @Override
    protected void onResume() {
        super.onResume();
       // if (isNight != SpUtil.isNight()) reload();
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    public void reload(){
        Intent intent = getIntent();
        overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();
        overridePendingTransition(0, 0);
        startActivity(intent);
    }
    public void showSnackBar(String message){
        SnackBar.make(App.getAppContext()).actionText(message).duration(Snackbar.LENGTH_SHORT).show();
    }
    public abstract int getLayoutId();

    public abstract void initView();



    public void showLoadingDialog(String titleText) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
            mLoadingDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.colorPrimary));
            mLoadingDialog.setCancelable(false);
            mLoadingDialog.setTitleText(""+titleText);
        }
        mLoadingDialog.show();
    }

    public void dismissLoadingDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

}
